﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vxl.Drawables;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Xml;

namespace Vxl.Utils
{
    public class VxlSerializer
    {
        public static VxlObject LoadVxlObject(string path){
            VxlObject vxlObject = null;
            FileStream fs = null;
            XmlDictionaryReader reader = null;
            try
            {
                if (File.Exists(path) == true)
                {
                    /*FileStream file = new FileStream(path, FileMode.Open);

                    BinaryFormatter bf = new BinaryFormatter();
                    vxlObject = bf.Deserialize(file) as VxlObject;*/
                    //vxlObject = ObjectXMLSerializer<VxlObject>.Load(path);

                    fs = new FileStream(path, FileMode.Open);
                    reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
                    DataContractSerializer ser = new DataContractSerializer(typeof(VxlObject));

                    // Deserialize the data and read it from the instance.
                    vxlObject = (VxlObject)ser.ReadObject(reader, true);

                    if (vxlObject == null)
                    {
                        MessageBox.Show("Unable to load customer object from file '" + path + "'!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show(path + " does not exist!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            finally{
                if(reader != null)
                    reader.Close();
                if(fs != null)
                    fs.Close();
            }

            return vxlObject;
        }

        public static bool SaveVxlObject(string path, ref VxlObject vxlObject){
            FileStream writer = null;
            try
            {
                /*Stream a = File.OpenWrite(path);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(a, vxlObject);
                a.Close();*/

                writer = new FileStream(path, FileMode.Create);
                DataContractSerializer ser =
                    new DataContractSerializer(typeof(VxlObject));
                ser.WriteObject(writer, vxlObject);
                

                //ObjectXMLSerializer<VxlObject>.Save(vxlObject, path);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to save vxlObject!" + Environment.NewLine + Environment.NewLine + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            finally{
                if(writer != null)
                    writer.Close();
            }

            return true;
        }

    }
}
